﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections.Specialized;
using CH.Easy.UI.Common;
using CH.Easy.UI.Domain;
using System.Web.Hosting;
using VTemplate.Engine;
using System.IO;
using CH.Easy.DbProvider.Mapping;
using CH.Easy.UI.Domain.ConfigAgg;

namespace CH.Easy.UI.Domain.PageAgg
{
   [TableMapping(Name = "PageConfig", PrimaryKey = "ID", IsIdentity = true)]
    public class BasePage:Entity
    {

        public int ID { get; set; }
        /// <summary>
        ///页面名字
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 页面类型
        /// </summary>
        public int Type { get; set; }
        /// <summary>
        /// 页面真实地址
        /// </summary>
        public string Url { get; set; }
        /// <summary>
        /// 页面重写地址
        /// </summary>
        public string RewriteUrl { get; set; }
        /// <summary>
        /// 页面布局文件路径
        /// </summary>
        public string PageLayoutPath { get; set; }
        /// <summary>
        /// 页面标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 页面SEO标题
        /// </summary>
        public string SEOTitle { get; set; }
        /// <summary>
        /// 页面SEO描述
        /// </summary>
        public string SEODescription { get; set; }
        /// <summary>
        /// 页面添加信息
        /// </summary>
        public DateTime AddTime { get; set; }
        /// <summary>
        /// 页面最后一次修改时间
        /// </summary>
        public DateTime LastModifyTime { get; set; }





        public SiteConfig SiteConfig { get; set; }
        protected HttpContext HttpContext { get; set; }
        protected PageLayout PageLayout { get; set; }
        public Dictionary<string, object> Data = new Dictionary<string, object>();
        protected virtual void SetData(string key, object value)
        {
            if (this.Data.ContainsKey(key))
            {
                throw new ArgumentException("存在重复的key:" + key);
            }

            Data.Add(key, value);

        }
        public BasePage()
        {
            
        }
        public BasePage Create(HttpContext httpContext, SiteConfig siteConfig) 
        {
            BasePage basePage = null;
            switch ((PageType)this.Type)
            {
                case PageType.Static:
                    basePage = new StaticPage();
                    break;
                case PageType.Dynamic:
                    basePage = new DynamicPage();
                    break;
                default:
                    break;
            }

            if (basePage != null) 
            {
                basePage.AddTime = this.AddTime;
                basePage.ID = this.ID;
                basePage.LastModifyTime = this.LastModifyTime;
                basePage.Name = this.Name;
                basePage.PageLayoutPath = this.PageLayoutPath;
                basePage.RewriteUrl = this.RewriteUrl;
                basePage.SEODescription = this.SEODescription;
                basePage.SEOTitle = this.SEOTitle;
                basePage.Title = this.Title;
                basePage.Type = this.Type;
                basePage.Url = this.Url;
                basePage.HttpContext = httpContext;
                basePage.SiteConfig = siteConfig;
            }



            return basePage;
        }
        public virtual void Render()
        {
            //加载页面布局
            this.PageLayout = new PageLayout(this.SiteConfig.FullSkinPath + this.PageLayoutPath, this);
            TemplateDocument document = new TemplateDocument(this.PageLayout.GetTemplate().ToString());
            foreach (var item in this.Data)
            {
                document.Variables.SetValue(item.Key, item.Value);
            }
            document.Render(this.HttpContext.Response.Output);
        }
    }
}
